package wiki.hitime.stl.daq.web.interceptor;

import wiki.hitime.stl.daq.bean.base.RespDTO;
import wiki.hitime.stl.daq.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author zhangdaoping
 * @create 2018/3/19 下午1:26
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 业务异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public RespDTO serviceExceptionHandler(ServiceException ex) {
        log.error("业务异常异常, message:{}", ex.getMessage(), ex);
        return RespDTO.fail(ex.getMessage());
    }

    /**
     * 运行时异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public RespDTO runtimeExceptionHandler(RuntimeException ex) {
        log.error("运行时异常, message:{}", ex.getMessage(), ex);
        return RespDTO.error(ex.getMessage());
    }

    /**
     * 其他错误
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public RespDTO exceptionHandler(Exception ex) {
        log.error("其他错误, message:{}", ex.getMessage(), ex);
        return RespDTO.error(ex.getMessage());
    }
}
